<script setup lang="ts">
import { loginApi } from '@/api/login'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

// 获取路由实例
const router = useRouter()

// 响应式数据
const loginForm = ref({
  username: '',
  password: '',
})

// 登录方法
const handleLogin = async () => {
  try {
    const result = await loginApi(loginForm.value)
    if (result.code) {
      // 登录成功
      ElMessage.success('登录成功')
      //将token存储到localStorage，确保userInfo为对象且有token字段
      localStorage.setItem('userInfo', JSON.stringify({ token: result.data }))
      //跳转页面
      router.push('/index')
    } else {
      // 登录失败
      ElMessage.error('用户名或密码错误')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error('登录失败，请稍后重试')
  }
}

// 重置方法
const handleReset = () => {
  loginForm.value.username = ''
  loginForm.value.password = ''
}
</script>

<template>
  <div class="login-container">
    <div class="login-background">
      <div class="background-shapes">
        <div class="shape shape-1"></div>
        <div class="shape shape-2"></div>
        <div class="shape shape-3"></div>
        <div class="shape shape-4"></div>
        <div class="shape shape-5"></div>
      </div>
    </div>

    <div class="login-content">
      <div class="login-card">
        <div class="login-header">
          <div class="logo">
            <div class="logo-icon">AI</div>
          </div>
          <h1>金蛛AI管理系统</h1>
          <p>欢迎回来，请登录您的账户</p>
        </div>

        <form class="login-form" @submit.prevent="handleLogin">
          <div class="form-group">
            <div class="input-wrapper">
              <svg
                class="input-icon"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
              >
                <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path>
                <circle cx="12" cy="7" r="4"></circle>
              </svg>
              <input
                v-model="loginForm.username"
                type="text"
                placeholder="用户名"
                class="form-input"
                required
                title="请输入用户名"
                oninvalid="this.setCustomValidity('请输入用户名')"
                oninput="this.setCustomValidity('')"
              />
            </div>
          </div>

          <div class="form-group">
            <div class="input-wrapper">
              <svg
                class="input-icon"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
              >
                <path d="M22 11H2"></path>
                <path
                  d="M5 2h14a1 1 0 0 1 1 1v18a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V3a1 1 0 0 1 1-1z"
                ></path>
              </svg>
              <input
                v-model="loginForm.password"
                type="password"
                placeholder="密码"
                class="form-input"
                required
                title="请输入密码"
                oninvalid="this.setCustomValidity('请输入密码')"
                oninput="this.setCustomValidity('')"
              />
            </div>
          </div>

          <div class="form-actions">
            <button type="submit" class="btn btn-primary">
              <span>登录</span>
              <svg
                class="btn-icon"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
              >
                <path d="M5 12h14"></path>
                <path d="m12 5 7 7-7 7"></path>
              </svg>
            </button>
            <button type="button" @click="handleReset" class="btn btn-secondary">重置</button>
          </div>
        </form>

        <div class="login-footer">
          <p>© 2024 金蛛AI管理系统. All rights reserved.</p>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  min-height: 100vh;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
  overflow: hidden;
  animation: gradientShift 15s ease-in-out infinite;
}

@keyframes gradientShift {
  0%,
  100% {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
  }
  25% {
    background: linear-gradient(135deg, #4facfe 0%, #00f2fe 50%, #667eea 100%);
  }
  50% {
    background: linear-gradient(135deg, #a8edea 0%, #fed6e3 50%, #667eea 100%);
  }
  75% {
    background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 50%, #667eea 100%);
  }
}

.login-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.login-background::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image:
    radial-gradient(circle at 20% 80%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 40% 40%, rgba(255, 255, 255, 0.05) 0%, transparent 50%);
  animation: particleFloat 20s ease-in-out infinite;
}

@keyframes particleFloat {
  0%,
  100% {
    transform: translateY(0px) scale(1);
    opacity: 0.5;
  }
  50% {
    transform: translateY(-20px) scale(1.1);
    opacity: 0.8;
  }
}

.background-shapes {
  position: relative;
  width: 100%;
  height: 100%;
}

.shape {
  position: absolute;
  border-radius: 50%;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.15) 0%, rgba(255, 255, 255, 0.05) 100%);
  backdrop-filter: blur(15px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.shape-1 {
  width: 250px;
  height: 250px;
  top: 5%;
  left: 5%;
  animation:
    float 8s ease-in-out infinite,
    pulse 4s ease-in-out infinite;
}

.shape-2 {
  width: 180px;
  height: 180px;
  top: 55%;
  right: 10%;
  animation:
    float 10s ease-in-out infinite reverse,
    pulse 6s ease-in-out infinite;
}

.shape-3 {
  width: 120px;
  height: 120px;
  bottom: 15%;
  left: 15%;
  animation:
    float 12s ease-in-out infinite,
    pulse 5s ease-in-out infinite;
}

.shape-4 {
  width: 80px;
  height: 80px;
  top: 30%;
  right: 30%;
  animation:
    float 7s ease-in-out infinite reverse,
    pulse 3s ease-in-out infinite;
}

.shape-5 {
  width: 60px;
  height: 60px;
  bottom: 40%;
  right: 5%;
  animation:
    float 9s ease-in-out infinite,
    pulse 4s ease-in-out infinite;
}

@keyframes float {
  0%,
  100% {
    transform: translateY(0px) rotate(0deg) scale(1);
  }
  50% {
    transform: translateY(-30px) rotate(180deg) scale(1.1);
  }
}

@keyframes pulse {
  0%,
  100% {
    opacity: 0.6;
    transform: scale(1);
  }
  50% {
    opacity: 1;
    transform: scale(1.05);
  }
}

.login-content {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 420px;
  padding: 20px;
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(25px);
  border-radius: 24px;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
  padding: 48px 40px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
}

.login-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.6s ease;
}

.login-card:hover::before {
  left: 100%;
}

.login-card:hover {
  transform: translateY(-8px) scale(1.02);
  box-shadow: 0 35px 70px rgba(0, 0, 0, 0.2);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  margin-bottom: 24px;
}

.logo-icon {
  width: 64px;
  height: 64px;
  margin: 0 auto;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  font-weight: 700;
  box-shadow: 0 8px 24px rgba(102, 126, 234, 0.3);
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
}

.logo-icon::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #5a67d8 0%, #6b46c1 100%);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.logo:hover .logo-icon {
  transform: scale(1.1) rotate(5deg);
  box-shadow: 0 12px 32px rgba(102, 126, 234, 0.4);
}

.logo:hover .logo-icon::before {
  opacity: 1;
}

.login-header h1 {
  color: #2d3748;
  margin: 0 0 8px 0;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.login-header p {
  color: #718096;
  margin: 0;
  font-size: 16px;
  font-weight: 400;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.form-group {
  position: relative;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  position: absolute;
  left: 16px;
  width: 20px;
  height: 20px;
  color: #a0aec0;
  z-index: 2;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.form-input {
  width: 100%;
  padding: 16px 16px 16px 48px;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 400;
  background: rgba(255, 255, 255, 0.8);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  outline: none;
  color: #2d3748;
  position: relative;
}

.form-input:focus {
  border-color: #667eea;
  background: white;
  box-shadow:
    0 0 0 4px rgba(102, 126, 234, 0.15),
    0 8px 25px rgba(102, 126, 234, 0.1);
  transform: translateY(-2px);
}

.form-input:focus + .input-icon {
  color: #667eea;
  transform: scale(1.1);
}

.form-input:hover {
  border-color: #cbd5e0;
  background: rgba(255, 255, 255, 0.9);
  transform: translateY(-1px);
}

.form-input::placeholder {
  color: #a0aec0;
  font-weight: 400;
}

.form-actions {
  display: flex;
  gap: 16px;
  margin-top: 8px;
}

.btn {
  flex: 1;
  padding: 16px 24px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  outline: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  position: relative;
  overflow: hidden;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.3);
  position: relative;
  overflow: hidden;
}

.btn-primary::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s ease;
}

.btn-primary:hover::before {
  left: 100%;
}

.btn-primary:hover {
  transform: translateY(-3px) scale(1.02);
  box-shadow: 0 12px 30px rgba(102, 126, 234, 0.4);
  background: linear-gradient(135deg, #5a67d8 0%, #6b46c1 100%);
}

.btn-primary:active {
  transform: translateY(-1px) scale(1.01);
}

.btn-icon {
  width: 18px;
  height: 18px;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.btn-primary:hover .btn-icon {
  transform: translateX(6px) scale(1.1);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.8);
  color: #718096;
  border: 2px solid #e2e8f0;
  backdrop-filter: blur(10px);
}

.btn-secondary:hover {
  background: white;
  border-color: #cbd5e0;
  color: #4a5568;
  transform: translateY(-1px);
}

.login-footer {
  text-align: center;
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid rgba(226, 232, 240, 0.5);
}

.login-footer p {
  color: #a0aec0;
  font-size: 14px;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-content {
    padding: 16px;
  }

  .login-card {
    padding: 32px 24px;
  }

  .form-actions {
    flex-direction: column;
  }

  .btn {
    width: 100%;
  }

  .shape-1,
  .shape-2,
  .shape-3 {
    display: none;
  }
}

/* 加载动画 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-card {
  animation: fadeInUp 0.6s ease-out;
}
</style>
